package pers.cz.postgirl.adapter.eform;

import pers.cz.postgirl.adapter.AbstractTransformer;
import pers.cz.postgirl.config.EvenType;
import pers.cz.postgirl.core.EnhanceModel;
import pers.cz.postgirl.core.common.CpublicFuncCallBack;

import java.util.Map;

/**
 * @program: postgirl-client
 * @description: 数据库操作监听
 * @author: Cheng Zhi
 * @create: 2023-12-20 10:07
 **/
public class EformDbOperListenerPlugin extends AbstractTransformer {

    @Override
    public void loadEnhanceModel() {
        enhanceModel = EnhanceModel.builder()
                .classPattern("jef.database.support.DefaultDbOperListener")
                .methodPatterns(getMethodPattern())
                .build();
    }

    private EnhanceModel.MethodPattern[] getMethodPattern() {

        // 添加自定义插码内容
        EnhanceModel.MethodPattern.DefaultCodePattern afterInsertCodePattern = EnhanceModel.MethodPattern.DefaultCodePattern.builder()
                .beforeCode(getBeforeSrc())
                .returnCode(getEndSrc())
                .build();
        EnhanceModel.MethodPattern afterInsert = EnhanceModel.MethodPattern.builder()
                .methodName("afterInsert")
                .paramTypes(new String[] {"java.lang.String", "int", "jef.database.AbstractDbClient", "java.util.Map"})
                .evenTypes(new EvenType[] {EvenType.BEFORE, EvenType.RETURN})
                .defaultCodePattern(afterInsertCodePattern)
                .build();

        EnhanceModel.MethodPattern afterUpdate = EnhanceModel.MethodPattern.builder()
                .methodName("afterUpdate")
                .paramTypes(new String[] {"java.lang.String", "int", "jef.database.AbstractDbClient", "java.util.Map"})
                .evenTypes(new EvenType[] {EvenType.BEFORE, EvenType.RETURN})
                .build();

        EnhanceModel.MethodPattern afterDelete = EnhanceModel.MethodPattern.builder()
                .methodName("afterDelete")
                .paramTypes(new String[] {"java.lang.String", "int", "jef.database.AbstractDbClient", "java.util.Map"})
                .evenTypes(new EvenType[] {EvenType.BEFORE, EvenType.RETURN})
                .build();

        EnhanceModel.MethodPattern afterSelect = EnhanceModel.MethodPattern.builder()
                .methodName("afterSelect")
                .paramTypes(new String[] {"java.lang.String", "int", "jef.database.AbstractDbClient", "java.util.Map"})
                .evenTypes(new EvenType[] {EvenType.BEFORE, EvenType.RETURN})
                .build();

        return new EnhanceModel.MethodPattern[] {
                afterInsert,
                afterUpdate,
                afterDelete,
                afterSelect,
        };
    }


    /**
     * 在方法返回前插码
     * @return
     */
    private CpublicFuncCallBack getBeforeSrc() {
        return new CpublicFuncCallBack() {

            @Override
            public String execute() throws Exception {
                return null;
            }

            @Override
            public CpublicFuncCallBack setParamters(Map<String, Object> map) throws Exception {
                return null;
            }
        };
    }

    /**
     * 在方法返回后插码
     * @return
     */
    private CpublicFuncCallBack getEndSrc() {

        return new CpublicFuncCallBack() {
            @Override
            public String execute() throws Exception {
                return null;
            }

            @Override
            public CpublicFuncCallBack setParamters(Map<String, Object> map) throws Exception {
                return null;
            }
        };
    }

}
